package pessoas
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import mx.core.FlexSprite;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSave;
	import org.flixel.FlxState;
	import org.flixel.FlxSprite;
	
	/**
	 * ...
	 * @author Lucas Mello
	 */
	public class Pessoa extends FlxSprite
	{		
		protected var speed_x:Number = 50;
		protected var speed_y:Number = 10;
		
		public var head:FlxSprite;
		public var body:FlxSprite;
		public var legs:FlxSprite;
		public var arms:FlxSprite;
		
		public var dead:Boolean = false;
		
		public var state:MainState;
		
		public function Pessoa():void
		{
			super(-1000, -1000);
			makeGraphic(32, 48, 0xffff0000);
		}
		
		public function init(_state:MainState, pos:FlxPoint, dir:int, _head:Class, _body:Class, _legs:Class, _arms:Class):void
		{	
			reset(pos.x, pos.y);
			
			state = _state;
			
			if (_body)
				body = new FlxSprite(-1000, -1000, _body);
				
			if (_head)
				head = new FlxSprite(-1000, -1000, _head);
			
			if (_legs)
			{
				legs = new FlxSprite(-1000, -1000);
				
				legs.loadGraphic(_legs, true, false, 32, 64);
				legs.addAnimation("walk", [0, 1, 2, 3, 4, 5, 7], 4, true);
				
				legs.play("walk", false);
			}
			
			if (_arms)
			{
				arms = new FlxSprite(-1000, -1000);
				
				arms.loadGraphic(_arms, true, false, 32, 64);
				arms.addAnimation("walk", [0, 1, 2, 3, 4, 5, 7], 4, true);
				
				arms.play("walk", false);
			}
			
			velocity.x = speed_x * dir;
			velocity.y = speed_y * dir;
			setScale(dir, scale.y);
			
			solid = true;
			
			if (Math.random() < 0.5)
			{
				velocity.y *= -1;
			}
		}
		
		override public function draw():void
		{
			if (legs) legs.draw();
			if (body) body.draw();
			if (head) head.draw();
			if (arms) arms.draw();
		}
		
		public override function postUpdate():void
		{
			super.postUpdate();
			if (legs) legs.postUpdate();
			if (body) body.postUpdate();
			if (head) head.postUpdate();
			if (arms) arms.postUpdate();
		}
		
		override public function update():void 
		{	
			if (legs)
			{
				legs.x = x;
				legs.y = y - 16;
				legs.alpha = alpha;
			}
			if (body)
			{
				body.x = x;
				body.y = y - 16;
				body.alpha = alpha;
			}
			if (head)
			{
				head.x = x;
				head.y = y - 16;
				head.alpha = alpha;
			}
			if (arms)
			{
				arms.x = x;
				arms.y = y - 16;
				arms.alpha = alpha;
			}
			
			if (dead)
			{
				this.alpha -= 0.05;
				
				if (this.alpha < 0)
					kill();
				
				return;
			}
			
			super.update();
			
			if (x < -100 + state.linha4.x || x > 1000 + state.linha5.x)
				kill();
		}
		
		public function die():void
		{
			dead = true;
		}
		
		public function setScale(sx:Number, sy:Number):void
		{
			scale.x = sx;
			scale.y = sy;
			
			if (legs)
			{
				legs.scale.x = sx;
				legs.scale.y = sy;
			}
			
			if (arms)
			{
				arms.scale.x = sx;
				arms.scale.y = sy;
			}
			
			if (head)
			{
				head.scale.x = sx;
				head.scale.y = sy;
			}
			
			if (body)
			{
				body.scale.x = sx;
				body.scale.y = sy;
			}
		}
	}
}